package com.example.medicalretrieval.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;


import com.example.medicalretrieval.BrowsingHistoryActivity;
import com.example.medicalretrieval.MyApplication;
import com.example.medicalretrieval.R;
import com.example.medicalretrieval.dao.BrowsingHistoryDao;
import com.example.medicalretrieval.pojo.BrowsingHistory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class BrowsingHistoryAdapter extends BaseAdapter {
    private List<BrowsingHistory> mBrowsingHistoryList = null;
    private Context mContext = null;
    public BrowsingHistoryAdapter(Context context,List<BrowsingHistory> browsingHistoryList){
        mBrowsingHistoryList = browsingHistoryList;
        mContext = context;
    }
    @Override
    public int getCount() {
        return mBrowsingHistoryList.size();
    }

    @Override
    public Object getItem(int position) {
        return mBrowsingHistoryList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_browsing,null);
            holder = new ViewHolder();
            holder.title = convertView.findViewById(R.id.ItemBrowsingTitle);
            holder.date = convertView.findViewById(R.id.ItemBrowsingDate);
            holder.date = convertView.findViewById(R.id.ItemBrowsingAuthors);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        BrowsingHistory browsingHistory = mBrowsingHistoryList.get(position);

        holder.title.setText(browsingHistory.getTitle());
        Date date = browsingHistory.getDate();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        holder.date.setText(format.format(date));
        holder.authors.setText(browsingHistory.getAuthors());
        convertView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                dialog(browsingHistory,position);
                return false;
            }
        });
        return convertView;
    }

    private void dialog(BrowsingHistory browsingHistory,int position){
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("删除浏览记录");
        builder.setMessage("确认删除该条记录？");
        builder.setPositiveButton("确认", (dialog, which) -> {
            BrowsingHistoryDao browsingHistoryDao = MyApplication.getInstance().getBrowsingHistoryDataBase().browsingHistoryDao();
            browsingHistoryDao.delete(browsingHistory);
            mBrowsingHistoryList.remove(position);
            notifyDataSetChanged();
            Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show();
        });
        builder.setNegativeButton("取消", (dialog, which) -> {

        });
    }

    public final class ViewHolder{
        public TextView title;
        public TextView date;
        public TextView authors;
    }

}
